package com.cn.springcloudshop.common;

import java.io.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Util {
	public static InputStream getStringStream(String sInputString) {
		ByteArrayInputStream tInputStringStream = null;
		if (sInputString != null && !sInputString.trim().equals("")) {
			tInputStringStream = new ByteArrayInputStream(sInputString.getBytes());
		}
		return tInputStringStream;
	}

	// 读取properties
	public static Map<String, String> getPropertisValues(String filePath) {
		Properties properties = new Properties();
		Map<String, String> map = new HashMap<>();
		try {
			InputStream inputStream = new BufferedInputStream(new FileInputStream(filePath));
			properties.load(inputStream);
			Iterator<String> iterator = properties.stringPropertyNames().iterator();
			while (iterator.hasNext()) {
				String key = iterator.next();
				map.put(key, properties.get(key).toString());
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return map;
	}

	public static int[] getHashcode(List<String> list) {
		int[] is = new int[list.size()];
		for (int i = 0; i < list.size(); i++) {
			is[i] = list.get(i).hashCode() & 0x1f4;
		}
		return is;
	}

	// 判断一个字符串是否都为数字
	public static boolean isDigit(String strNum) {
		Pattern pattern = Pattern.compile("[0-9]{1,}");
		Matcher matcher = pattern.matcher((CharSequence) strNum);
		return matcher.matches();
	}

	public static int getHashcode(String str) {
		int is = str.hashCode() & 0x1f4;
		return is;
	}
	 
	//计算大概年龄
	 public static int getAboutAge(Date birthDay) throws Exception{ 
	        //获取当前系统时间
	        Calendar cal = Calendar.getInstance(); 
	        //如果出生日期大于当前时间，则抛出异常
	        if (cal.before(birthDay)) { 
	            throw new IllegalArgumentException( 
	                "The birthDay is before Now.It's unbelievable!"); 
	        } 
	        //取出系统当前时间的年、月、日部分
	        int yearNow = cal.get(Calendar.YEAR); 
	        int monthNow = cal.get(Calendar.MONTH); 
	        int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH); 
	        //将日期设置为出生日期
	        cal.setTime(birthDay); 
	        //取出出生日期的年、月、日部分  
	        int yearBirth = cal.get(Calendar.YEAR); 
	        int monthBirth = cal.get(Calendar.MONTH); 
	        int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH); 
	        //当前年份与出生年份相减，初步计算年龄
	        int age = yearNow - yearBirth; 
	        //当前月份与出生日期的月份相比，如果月份小于出生月份，则年龄上减1，表示不满多少周岁
	        if (monthNow <= monthBirth) { 
	            //如果月份相等，在比较日期，如果当前日，小于出生日，也减1，表示不满多少周岁
	            if (monthNow == monthBirth) { 
	                if (dayOfMonthNow < dayOfMonthBirth) age--; 
	            }else{ 
	                age--; 
	            } 
	        } 
	        System.out.println("age:"+age); 
	        return age; 
	    }
	 public static final String[] TIMESECTION = {"出生","1月龄","2月龄","3月龄","4月龄",
		    "5月龄","6月龄","8月龄","9月龄","11月龄","1周岁","1岁半","2周岁","3周岁","4周岁","6周岁","9周岁"};
	 public static String getAgeStage(String age){ 
		 String year = age.substring(0,age.indexOf("岁"));
		 String month;
		 month = age.substring(age.indexOf("岁") + 1,age.lastIndexOf("月"));
         String childAge = "";
		 //按月份
		 if(Integer.parseInt(year)<1){
		 //出生 
		 if(Integer.parseInt(month)<1){
		 childAge = "出生"; 
		 //一月龄
		 }else if(Integer.parseInt(month)>=1 && Integer.parseInt(month)<2){
		 childAge = "1月龄";
		 //二月龄
		 }else if(Integer.parseInt(month)>=2 && Integer.parseInt(month)<3){
		 childAge = "2月龄";
		 //三月龄
		 }else if(Integer.parseInt(month)>=3 && Integer.parseInt(month)<4){
		 childAge = "3月龄";
		 //四月龄
		 }else if(Integer.parseInt(month)>=4 && Integer.parseInt(month)<5){
		 childAge = "4月龄";
		 //五月龄
		 }else if(Integer.parseInt(month)>=5 && Integer.parseInt(month)<6){
		 childAge = "5月龄";
		 //六月龄
		 }else if(Integer.parseInt(month)>=6 && Integer.parseInt(month)<8){
		 childAge = "6月龄";
		 //八月龄
		 }else if(Integer.parseInt(month)>=8 && Integer.parseInt(month)<9){
		 childAge = "8月龄";
		 //九月龄
		 }else if(Integer.parseInt(month)>=9 && Integer.parseInt(month)<11){
		 childAge = "9月龄";
		 //十一月龄
		 }else if(Integer.parseInt(month)>=11 && Integer.parseInt(month)<12){
		 childAge = "11月龄";
		 }			 
		 //一周岁和一岁半	 
		 }else if(Integer.parseInt(year)>=1 && Integer.parseInt(year)<2){
		 //一周岁
		 if(Integer.parseInt(month)<6){
		 childAge = "1周岁";
		 //一岁半	 
		 }else{
		 childAge = "1岁半";
		 }
		 //2周岁
		 }else if(Integer.parseInt(year)>=2 && Integer.parseInt(year)<3){
		 childAge = "2周岁"; 
		 //3周岁 
		 }else if(Integer.parseInt(year)>=3 && Integer.parseInt(year)<4){
		 childAge = "3周岁";	 
		 //4周岁	 
		 }else if(Integer.parseInt(year)>=4 && Integer.parseInt(year)<6){
		 childAge = "4周岁";	 
		 //6周岁	 
		 }else if(Integer.parseInt(year)>=6 && Integer.parseInt(year)<9){
		 childAge = "6周岁";
		 //9周岁	 
		 }else{
		 childAge = "9周岁";	 
		 }
		return childAge;
	    }
	
	 public static String getAge(String birthDay) throws Exception { 
		Calendar c = Calendar.getInstance();
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");      
		Date date = format.parse(birthDay);
		c.setTime(date);
		//c.add(Calendar.MONTH, 1);
		String yue = String.valueOf(c.get(Calendar.MONTH)+1);
	 	Calendar birthday = new GregorianCalendar(c.get(Calendar.YEAR),Integer.parseInt(yue),c.get(Calendar.DAY_OF_MONTH));//2010年10月12日，month从0开始  
    //  Calendar birthday = new GregorianCalendar(2016, 8, 28);//2010年10月12日，month从0开始  
        Calendar now = Calendar.getInstance();  
        int day = now.get(Calendar.DAY_OF_MONTH) - birthday.get(Calendar.DAY_OF_MONTH); 
        int month = now.get(Calendar.MONTH)+1 - birthday.get(Calendar.MONTH);  
        int year = now.get(Calendar.YEAR) - birthday.get(Calendar.YEAR);  
        //按照减法原理，先day相减，不够向month借；然后month相减，不够向year借；最后year相减。  
        if(day<0){  
            month -= 1;  
            now.add(Calendar.MONTH, -1);//得到上一个月，用来得到上个月的天数。  
            day = day + now.getActualMaximum(Calendar.DAY_OF_MONTH);  
        }  
        if(month<0){  
            month = (month+12)%12;  
            year--;  
        }  
		return year+"岁"+month+"月"+day+"天";  
	 }
        public static void main(String[] args) throws Exception{ 
        	Util.getAge("2016-08-28");
    }  
}
